package com.senseilearning.sensei;

import java.util.Locale;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.viewpagerindicator.PageIndicator;
import com.viewpagerindicator.TitlePageIndicator;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

public class SenseiActivity extends SherlockFragmentActivity {

	private ViewPager mPager;
	private PageIndicator mIndicator;
	private TestFragmentAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (!Chapters.hasData()) {
			new XMLFileReader(this).readChapters();
		}
		setContentView(R.layout.activity_main);
		mAdapter = new TestFragmentAdapter(getSupportFragmentManager());
		mPager = (ViewPager) findViewById(R.id.pager);
		mPager.setAdapter(mAdapter);
		mIndicator = (TitlePageIndicator) findViewById(R.id.indicator);
		mIndicator.setViewPager(mPager);
	}

	class TestFragmentAdapter extends FragmentPagerAdapter {
		private int mCount = Chapters.numberOfChapters();

		public TestFragmentAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			Bundle data = new Bundle();
			data.putInt("chapter", position);
			Fragment fragment = new GridViewFragment();
			fragment.setArguments(data);
			return fragment;
		}

		@Override
		public int getCount() {
			return mCount;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return Chapters.getChapterNames().get(position)
					.toUpperCase(Locale.ENGLISH);
		}
	}

}